package com.jy.api.controller.sales;

import com.jy.api.dao.StoreDao;
import com.jy.api.domain.Store;
import com.jy.api.vo.sales.StoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2017/9/11.
 */
@Slf4j
@RestController
@RequestMapping(value = "v1/sales", produces = "application/json;charset=utf-8")
public class StoreInfoSalesController {

    @Autowired
    StoreDao storeDao;

    /**
     * 获取邀请码对应店铺信息
     *
     * @param codes
     * @return
     */
    @RequestMapping(value = "/stores", method = RequestMethod.POST)
    public ResponseEntity getSalesStoreInfo(@PageableDefault(value = 10, sort = {"id"}, direction = Sort.Direction.DESC)
                                                    Pageable pageable,
                                            @RequestBody List<String> codes) {
        try {
            Page<Store> stores = storeDao.findListByReferralCodeIn(codes, pageable);
            StoreVo storeVo = new StoreVo(stores.getTotalPages(), stores.getTotalElements());
            stores.getContent().stream().map(e ->
                    storeVo.getStores().add(new StoreVo.Store(e.getId(), e.getName(), e.getExpireTime(), e.getReferralCode(),
                            0))
            ).collect(Collectors.toList());
            return new ResponseEntity(storeVo, HttpStatus.OK);
        } catch (Exception ex) {
            log.error("getSalesStoreInfo is error:codes{}", codes, ex);
            return new ResponseEntity("系统运行错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
